package cwinsor.com.leaf_raking;

import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.RowSorter;
import javax.swing.SortOrder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.TableColumnModelListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class JTableData extends JTable /* implements TableModelListener */ {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTable jTableSummary;
	private TableModelData tableModelData;

	/*
	 * constructor
	 */
	public JTableData(TableModelData tableModel) {
		super(tableModel);
		this.tableModelData = tableModel;  // duplicate - but derived class

		// ok - we just set the table model above...
		// but it resides in here (the parent class)...
		// so get it
		// and we want to add a listener (callback) routine
		// for tableChanged event - this routine is defined below
		TableModel tM = getModel();
		tM.addTableModelListener(this);
	}

	/*
	 * get TableModelData
	 */
	protected TableModelData getTableModelData() {
		return tableModelData;
	}

	/*
	 * default setup for size, etc
	 */
	public void setup() {
	///	setPreferredScrollableViewportSize(new Dimension(9000, 300));
		setAutoCreateRowSorter(true);
		//	setColumnSelectionAllowed(true);
		//	setFocusable(true);
		 setFillsViewportHeight(true);

		//	setRowSelectionInterval(0, 0);
		//	setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		//setSelectionModel(new ForcedListSelectionModel());

		//setRowSelectionAllowed(true);
		//setColumnSelectionAllowed(true);
		//	setCellSelectionAllowed(false);

		// create sort on first two columns
		//List <RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>();
		//sortKeys.add(new RowSorter.SortKey(getTableModelData().colFirstName, SortOrder.ASCENDING));
		//sortKeys.add(new RowSorter.SortKey(getTableModelData().colLastName, SortOrder.ASCENDING));
		//sortKeys.add(new RowSorter.SortKey(getTableModelData().colAvailTotalChange, SortOrder.ASCENDING));
		//sortKeys.add(new RowSorter.SortKey(getTableModelData().colTotalHappinessCurrent, SortOrder.ASCENDING));
		//sortKeys.add(new RowSorter.SortKey(getTableModelData().colTotalHappinessProposed, SortOrder.ASCENDING));
		//sortKeys.add(new RowSorter.SortKey(1, SortOrder.ASCENDING));
		//sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING));
		//sortKeys.add(new RowSorter.SortKey(10, SortOrder.ASCENDING));

		//TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(this.getModel());
		//sorter.setSortKeys(sortKeys); 
		//this.setRowSorter(null);
		//this.setRowSorter(sorter);
	}

	// main table manages column widths of the summary table - give the necessary info
	public void manageSummaryTableColumnEvents(JTable jTableSummary) {
		this.jTableSummary = jTableSummary;
	}


	/*
	 * events relating to column resizing
	 * this is where we manage the summary table column sizes
	 */
	@Override
	public void columnSelectionChanged(ListSelectionEvent e) {
	}

	@Override
	public void columnRemoved(TableColumnModelEvent e) {
	}

	@Override
	public void columnMoved(TableColumnModelEvent e) {
	}

	@Override
	public void columnMarginChanged(ChangeEvent e) {
		TableColumnModel tableColumnModel = getColumnModel();
		TableColumnModel footerColumnModel = jTableSummary.getColumnModel();
		for (int i = 0; i < tableColumnModel.getColumnCount(); i++) {
			int w = tableColumnModel.getColumn(i).getWidth();
			footerColumnModel.getColumn(i).setMinWidth(w);
			footerColumnModel.getColumn(i).setMaxWidth(w);
			// footerColumnModel.getColumn(i).setPreferredWidth(w);
		}
		//	doLayout();
		jTableSummary.repaint();
		repaint();
	}

	@Override
	public void columnAdded(TableColumnModelEvent e) {
	}

	//	@Override
	//	public void tableChanged(TableModelEvent e) {
	//		Msg.note(dS + ": received event tableChanged");	
	//			   int row = e.getFirstRow();
	//		        int column = e.getColumn();
	//		        TableModel model = (TableModel)e.getSource();
	//		        String columnName = model.getColumnName(column);
	//		        Object data = model.getValueAt(row, column);
	//	}

	// debug
	String dS = this.getClass().getSimpleName();
}
